package com.qianguo.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eva.androidlibrary.utils.IconfontUtil;
import com.eva.androidlibrary.widget.tabhost.FragmentTabHostLayout;
import com.eva.androidlibrary.widget.tabhost.FragmentTabHostLayoutAdapter;
import com.eva.androidlibrary.widget.tabhost.TabHostLayout;
import com.qianguo.shop.R;
import com.qianguo.shop.base.BaseChooseImageActivity;
import com.qianguo.shop.entity.event.MainTabEvent;
import com.qianguo.shop.fragment.tab.IMContactsTabFragment_;
import com.eva.androidlibrary.icon.FruitIcon;
import com.qianguo.shop.fragment.tab.MyCenterTabFragment_;
import com.qianguo.shop.fragment.tab.OrderTabFragment_;
import com.qianguo.shop.fragment.tab.ShopTabFragment_;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


/**
 * 卖家首页
 *
 * @author GUXIA318
 * @date 2015-09-15 22:02
 */
@EActivity(R.layout.activity_main_shop)
public class ShopMainActivity extends BaseChooseImageActivity {

    private static final int[] TABS_NAME = new int[] {R.string.tab_order, R.string.tab_shop, R.string.tab_message, R.string.tab_me};
    private static final FruitIcon[] TABS_ICON = new FruitIcon[] {FruitIcon.TAB_ORDER, FruitIcon.TAB_SHOP, FruitIcon.TAB_MESSAGE, FruitIcon.TAB_MY_CENTER};
    private static final FruitIcon[] TABS_ICON_SELECTED = new FruitIcon[] {FruitIcon.TAB_ORDER, FruitIcon.TAB_SHOP_SELECTED, FruitIcon.TAB_MESSAGE_SELECTED, FruitIcon.TAB_MY_CENTER_SELECTED};

    @ViewById(R.id.fragment_tab_host_layout)
    FragmentTabHostLayout mFragmentTabHostLayout;
    private FragmentTabHostLayoutAdapter mAdapter;

    public static void jumpToMain(Context context) {
        context.startActivity(new Intent(context, com.qianguo.shop.activity.ShopMainActivity_.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEvent(MainTabEvent event) {
        mFragmentTabHostLayout.setTabSelected(event.getTabIndex());
    }

    @Override
    protected void initializeUI() {
        mAdapter = new TabHostAdapter(R.id.fragment_container, getSupportFragmentManager());
        mFragmentTabHostLayout.setAdapter(mAdapter);
        mFragmentTabHostLayout.setTabSelected(0);
        mFragmentTabHostLayout.setOnTabHostItemSelectedListener(new TabHostLayout.OnTabHostSelectedChangedListener() {
            @Override
            public void onSelected(View view, int position) {
                setToolBarTitle(getString(TABS_NAME[position]));
            }
        });
    }

    @Override
    protected String getToolbarTitle() {
        return null;
    }

    @Override
    protected boolean hasNavigationIcon() {
        return false;
    }

    private class TabHostAdapter extends FragmentTabHostLayoutAdapter {

        public TabHostAdapter(int containerId, FragmentManager fm) {
            super(containerId, fm);
        }

        @Override
        public int getCount() {
            return TABS_NAME.length;
        }

        @Override
        public View getTab(int position, boolean isSelected, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(ShopMainActivity.this).inflate(R.layout.activity_tab_business, null);
            }

            TextView icon = (TextView) convertView.findViewById(R.id.business_tab_icon);
            if (isSelected) {
                IconfontUtil.setIcon(ShopMainActivity.this, icon, TABS_ICON_SELECTED[position]);
            } else {
                IconfontUtil.setIcon(ShopMainActivity.this, icon, TABS_ICON[position]);
            }

            TextView name = (TextView) convertView.findViewById(R.id.business_tab_text);
            name.setText(getString(TABS_NAME[position]));
            return convertView;
        }

        @Override
        public Fragment getFragmentInstance(int position) {
            Fragment f = null;
            switch (position) {
                case MainTabEvent.TAB_ORDER:
                    f = new OrderTabFragment_();
                    break;
                case MainTabEvent.TAB_SHOP:
                    f = new ShopTabFragment_();
                    break;
                case MainTabEvent.TAB_MESSAGE:
                    f = new IMContactsTabFragment_();
                    break;
                case MainTabEvent.TAB_MY_CENTER:
                    f = new MyCenterTabFragment_();
                    break;
            }
            return f;
        }
    }
}
